package cn.com.pc.content.jpa;

import cn.com.pc.content.domain.metadata.ContentDataType;

import javax.persistence.AttributeConverter;
import java.util.HashMap;
import java.util.Map;


public class ContentDataTypeConverter implements AttributeConverter<ContentDataType, String> {
    final Map<String, ContentDataType> map;

    public ContentDataTypeConverter() {
        map = new HashMap<>();
        for (ContentDataType s : ContentDataType.values()) {
            map.put(s.name(), s);
        }
    }

    @Override
    public String convertToDatabaseColumn(ContentDataType attribute) {
        return attribute == null ? null : attribute.name();
    }

    @Override
    public ContentDataType convertToEntityAttribute(String dbData) {
        return map.getOrDefault(dbData, null);
    }
}
